<?php
if(isset($_POST['submit'])){
	/*				/**
	**	F O R M			 **
	**	    V A R I A B L E S	 **
	*/				/**/
	$problem = false;
	
	// Member type
	$member_type = $_POST['type'];
	
	// Name variable
	if(isset($_POST['org_name']) && !empty($_POST['org_name'])){
		$name = mysql_real_escape_string($_POST['org_name']);
	}else if(isset($_POST['org_name']) && empty($_POST['org_name'])){
		$problem = true;
		$content .= '
		<P class="error">You must enter the organization\'s name</P>
		';
	}else if(isset($_POST['first_name']) && !empty($_POST['first_name']) && !empty($_POST['last_name'])){
		$name = mysql_real_escape_string($_POST['first_name']). ' ';
		if(!empty($_POST['middle_initial'])){
			$name.= mysql_real_escape_string($_POST['middle_initial']).'. ';
		}
		$name .= mysql_real_escape_string($_POST['last_name']);
		if(!empty($_POST['suffix'])){
			$name.= ' '.mysql_real_escape_string($_POST['suffix']);
		}
		
	}else if(isset($_POST['first_name'])){
		if(empty($_POST['first_name'])){
			$problem = true;
			$content .= '
			<P class="error">You must enter your first name.</P>
			';
		}
		if(empty($_POST['last_name'])){
			$problem = true;
			$content .= '
			<P class="error">You must enter your last name.</P>
			';
		}
	}
	
	// Username 
	$username = mysql_real_escape_string($_POST['username']);
	$username_exists = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username='$username'"));
	
	if($username_exists > 0){
		$problem = true;
		$content .= '
		<P class="error">You username already exists. Please try another.</P>
		';
	}
	
	// Password
	$password = mysql_real_escape_string($_POST['password']);
	$verify_password = mysql_real_escape_string($_POST['chk_password']);
	
	if(empty($password)){
		$problem = true;
		$content .= '
		<P class="error">Please enter a password.</P>
		';
	}else if($password != $verify_password){
		$problem = true;
		$content .= '
		<P class="error">Your passwords do not match. Please re-enter them.</P>
		';
	}else{
		$password = md5($password);
	}
	
	// Email
	$email = mysql_real_escape_string($_POST['email']);
	$email_exists = mysql_num_rows(mysql_query("SELECT * FROM users WHERE email='$email'"));
	if($email_exists > 0){
		$problem = true;
		$content .= '
		<P class="error">Your email exists already. You cannot register again.</P>
		';
	}
	if(empty($email)){
		$problem = true;
		$content .= '
		<P class="error">Please enter an email address.</P>
		';
	}
	
	// Website
	$website = mysql_real_escape_string($_POST['website']);
	if(!empty($website)){
		$website = 'http://'.$website;
	}
	
	// Birthday
	if(!empty($_POST['month']) || !empty($_POST['day']) || !empty($_POST['year'])){
		if(empty($_POST['month']) || empty($_POST['day']) || empty($_POST['year'])){
			$problem = true;
			$content .= '
			<P class="error">Please enter your birth-date correctly</P>
			';
		}else if((date('Y') - $_POST['year']) < 18){
			$problem = true;
			$content .= '
			<P class="error">You must be at least 18 years old</P>
			';
		}else{
			$birthday = mysql_real_escape_string($_POST['month']).'/'.mysql_real_escape_string($_POST['day']).'/'.mysql_real_escape_string($_POST['year']);
		}
	}
	
	// Gender
	$gender = mysql_real_escape_string($_POST['gender']);
	
	if($member_type == 'IND' && empty($gender)){
		$problem = true;
		$content .= '
		<P class="error">Please select a gender.</P>
		';
	}
	
	// Phone number
	if(!empty($_POST['area_code']) && !empty($_POST['phone_3']) && !empty($_POST['phone_4'])){
		$phone_number = '('.mysql_real_escape_string($_POST['area_code']).') '.mysql_real_escape_string($_POST['phone_3']).' - '.mysql_real_escape_string($_POST['phone_4']);
	}else if(empty($_POST['area_code']) && empty($_POST['phone_3']) && empty($_POST['phone_4'])){
		// Do Nothing	
	// Some parts are missing, but not all
	}else{
		$problem = true;
		$content .= '
		<P class="error">Please enter all parts of your phone number if you want it included in our database.</P>
		';
	}
	
	// Address
	$address_line1 = mysql_real_escape_string($_POST['address1']);
	$address_line2 = mysql_real_escape_string($_POST['address2']);
	if(empty($address_line1)){
		$problem = true;
		$content .= '
		<P class="error">You need to include your address to participate in our network.</P>
		';
	}
	
	// City
	$city = mysql_real_escape_string($_POST['city']);
	if(empty($city)){
		$problem = true;
		$content .= '
		<P class="error">You need to include your city to participate in our network.</P>
		';
	}
	
	// State
	$state = mysql_real_escape_string($_POST['state']);
	if(empty($state)){
		$problem = true;
		$content .= '
		<P class="error">You need to include your state to participate in our network.</P>
		';
	}
 
 	// Zipcode
 	$zipcode = mysql_real_escape_string($_POST['zip']);
 	if(empty($zipcode)){
 		$problem = true;
		$content .= '
		<P class="error">You need to include your address to participate in our network.</P>
		';
	}
	
	// Make USERID
	$userid = make_unique_id($email);
		
	// If there is no problem,
	//	insert into mysql database
	if(!$problem){
		
		$query = mysql_query("INSERT INTO users VALUES('','$userid','$member_type','$name','$username','$password','$email','$website','$birthday','$gender','$phone_number','$address_line1','$address_line2','$city','$state','$zipcode')");
		
		$query2 = mysql_query("INSERT INTO profiles VALUES('','$userid','','','','','','','0','0','0','','','0')");
		
		
		// Log the user in
		session_name("Login");
		session_start();
		$stats = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}'"));
		$_SESSION['username'] = $_POST['username'];
		$_SESSION['state'] = $stats['state'];
		$_SESSION['member_type'] = $stats['member_type'];
		$_SESSION['user_id'] = $stats['user_id'];
		$_SESSION['name'] = $stats['name'];
		
		mysql_close();
		
		refresh('/index.php');
	}
}

?>
